package com.kgc.scd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.Comment;
import com.kgc.scd.form.CommentForm;
import com.kgc.scd.form.ReviewForm;
import com.kgc.scd.service.CommentService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.SensitiveWordFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : #jiangguoqiang
 * <p>
 * Description: CommentController
 */
@Slf4j
@RestController
@Api(tags = "评论入口")
public class CommentController {

    @Autowired
    private CommentService commentService;


    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    /**
     * @author : Administrator
     * @date   : 2022/11/15
     * @param  : [comment, pageNo, pageSize]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 查询评论列表
     */
    @ApiOperation(value = "查询评论表",notes = "查询评论表")
    @GetMapping("/getCommentList")
    public PageSupport<Comment> getCommentList(@ApiParam(name = "goodsId",value = "评论") @RequestParam(required = false) String goodsId,
                                               @ApiParam(required = true,name = "pageNo",value = "当前页") @RequestParam(value="pageNo",defaultValue="1") Integer pageNo,
                                               @ApiParam(required = true,name = "pageSize",value = "条数") @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize){

        if (Objects.isNull(pageNo) || pageNo < -1){
            pageNo = 1;
        }
        if (Objects.isNull(pageSize)){
            pageSize = -1;
        }
        return commentService.getCommentListAndPage(goodsId, pageNo, pageSize);
    }


    /**
     * @author : Administrator
     * @date   : 2022/11/15
     * @param  : [comment]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 增加评论
     */
    @ApiOperation(value = "增加评论",notes = "增加评论")
    @PostMapping("/addCommentList")
    public RequestResult<?> addCommentList(@RequestBody CommentForm commentForm){
        // 校验搜索关键字中，是否包含敏感词，如果包含，提示错误
        if(sensitiveWordFilter.isContainSensitiveWordMin(commentForm.getContent())){
            log.warn("------ 命中敏感词，评论为：{} ------", commentForm.getContent());
            return ResultBuildUtil.fail("搜索失败，命中敏感词");
        }
        boolean commentList = commentService.addCommentList(commentForm);
        if (commentList){
            return ResultBuildUtil.success("评价成功");
        }
        return ResultBuildUtil.fail("999","评价失败，此订单状态不能评价");
    }

    /**
     * @author : Administrator
     * @date   : 2022/11/15
     * @param  : [reviewForm]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : 追加评论
     */
    @ApiOperation(value = "追加评论",notes = "追加评论")
    @PostMapping("/updateReviewList")
    public RequestResult<?> addReviewList(@RequestBody ReviewForm reviewForm){
        // 校验搜索关键字中，是否包含敏感词，如果包含，提示错误
        if(sensitiveWordFilter.isContainSensitiveWordMin(reviewForm.getReview())){
            log.warn("------ 命中敏感词，评论为：{} ------", reviewForm.getReview());
            return ResultBuildUtil.fail("搜索失败，命中敏感词");
        }
        boolean reviewList = commentService.updateReviewList(reviewForm);
        if (reviewList){
            return ResultBuildUtil.success("追评成功");
        }
        return ResultBuildUtil.fail("999","追评失败，此订单状态不能追评");
    }
}
